package com.ruoyi.web.controller.hbs;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hbs.domain.MafxInventoryTransferRecords;
import com.ruoyi.hbs.domain.MsfxBusinessStatistics;
import com.ruoyi.hbs.domain.MsfxTerminalStatistics;
import com.ruoyi.hbs.service.MafxInventoryTransferRecordsService;
import com.ruoyi.hbs.service.MsfxBusinessStatisticsService;
import com.ruoyi.hbs.service.MsfxTerminalStatisticsService;
import com.ruoyi.hbs.service.SingleSalesInventoryRecordsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wl
 * @date 2025年11月10日09:33
 */

@RestController
@RequestMapping("/hbs/statisticsHbs")
@Api(tags = "HBS数据采集管理")
@Slf4j
public class StatisticsHbsController extends BaseController {

    @Autowired
    private MsfxBusinessStatisticsService msfxBusinessStatisticsService;
    @Autowired
    private MsfxTerminalStatisticsService msfxTerminalStatisticsService;
    @Autowired
    private SingleSalesInventoryRecordsService singleSalesInventoryRecordsService;
    @Autowired
    private MafxInventoryTransferRecordsService mafxInventoryTransferRecordsService;

    /**
     * msfx- 按商业统计报表数据
     *
     * @return List<MsfxBusinessStatistics> 统计结果列表
     */
    @ApiOperation(value = "同步商业统计报表数据", notes = "同步商业统计报表数据")
    @GetMapping(value = "/syncMsfxBusinessStatisticsReport")
    @Log(title = "msfx-商业统计报表数据", businessType = BusinessType.COLLECT)
    public AjaxResult syncMsfxBusinessStatisticsReport() {
        try {
            List<MsfxBusinessStatistics> businessStatisticsReport = msfxBusinessStatisticsService.syncBusinessStatisticsReport();
        } catch (Exception e) {
            return AjaxResult.error("同步商业统计报表数据异常"+ e);
        }
        return AjaxResult.success();
    }

    /**
     * msfx- 按终端统计报表数据
     *
     * @return List<MsfxBusinessStatistics> 统计结果列表
     */
    @ApiOperation(value = "同步终端统计报表数据", notes = "同步码上放心终端统计报表数据")
    @GetMapping(value = "/syncMsfxTerminalStatisticsReport")
    @Log(title = "msfx-终端统计报表数据", businessType = BusinessType.COLLECT)
    public AjaxResult syncMsfxTerminalStatisticsReport() {
        try {
            List<MsfxTerminalStatistics> terminalStatisticsReport = msfxTerminalStatisticsService.syncTerminalStatisticsReport();
        } catch (Exception e) {
            return AjaxResult.error("同步终端统计报表数据异常"+ e);
        }
        return AjaxResult.success();
    }

    /**
     * msfx-出库补充统计
     */
    @ApiOperation(value = "同步出库补充统计数据", notes = "同步码上放心出库补充统计数据")
    @GetMapping(value = "/syncMsfxOutboundSupplementStatistics")
    @Log(title = "msfx-出库补充统计", businessType = BusinessType.COLLECT)
    public AjaxResult syncMsfxOutboundSupplementStatistics() {
        try {
            List<MafxInventoryTransferRecords> outboundSupplementStatistics = msfxTerminalStatisticsService.syncMsfxOutboundSupplementStatistics();
        } catch (Exception e) {
            return AjaxResult.error("同步出库补充统计数据异常"+ e);
        }
        return AjaxResult.success();
    }


    /**
     * TODO 实验处理历史数据
     * HBS- 聚合补充处理未匹配的记录
     */
    @ApiOperation(value = "聚合补充处理未匹配的记录", notes = "处理未匹配的销售库存记录数据")
    @GetMapping(value = "/dataMatchingTask")
    @Log(title = "HBS-聚合补充处理未匹配的记录", businessType = BusinessType.COLLECT)
    public AjaxResult dataMatchingTask() {
        try {
            singleSalesInventoryRecordsService.processUnmatchedRecordes();
        } catch (Exception e) {
            return AjaxResult.error("聚合补充处理未匹配的记录"+ e);
        }
        return AjaxResult.success();
    }


    /**
     * 查询MSFX—库存转移记录列表
     */
    @ApiOperation(value = "查询库存转移记录列表", notes = "分页查询MSFX库存转移记录列表")
    @PreAuthorize("@ss.hasPermi('hbs:statisticsHbs:list')")
    @GetMapping("/inventory/list")
    public TableDataInfo listInventoryTransferRecords(
            @ApiParam(value = "库存转移记录查询条件", required = false)
            MafxInventoryTransferRecords mafxInventoryTransferRecords) {
        startPage();
        LambdaQueryWrapper<MafxInventoryTransferRecords> queryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        if (mafxInventoryTransferRecords.getBusinessId() != null && !mafxInventoryTransferRecords.getBusinessId().isEmpty()) {
            queryWrapper.eq(MafxInventoryTransferRecords::getBusinessId, mafxInventoryTransferRecords.getBusinessId());
        }
        if (mafxInventoryTransferRecords.getOutboundQuantity() != null) {
            queryWrapper.eq(MafxInventoryTransferRecords::getOutboundQuantity, mafxInventoryTransferRecords.getOutboundQuantity());
        }
        if (mafxInventoryTransferRecords.getProductId() != null && !mafxInventoryTransferRecords.getProductId().isEmpty()) {
            queryWrapper.eq(MafxInventoryTransferRecords::getProductId, mafxInventoryTransferRecords.getProductId());
        }
        if (mafxInventoryTransferRecords.getInboundQuantity() != null) {
            queryWrapper.eq(MafxInventoryTransferRecords::getInboundQuantity, mafxInventoryTransferRecords.getInboundQuantity());
        }
        List<MafxInventoryTransferRecords> list = mafxInventoryTransferRecordsService.list(queryWrapper);
        return getDataTable(list);
    }
}
